<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/view/common/taglib.jsp" %>    
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

<script>
	//# sourceURL=orgFunctionList.js

	$(document).ready(function() {
		
	});

	function addFunction() {
		if (checkSelected('add') == false) return;
		
		$('#dialog_main').modal({
			keyboard: false,
			remote: '${ctx}/org/function/add.action?parentId=' + selFuncId + '&appId=${appId}'
		});
	}

	function editFunction() {
		if (checkSelected() == false) return;
				
		$('#dialog_main').modal({
			keyboard: false,
			remote: '${ctx}/org/function/modify.action?funcId=' + selFuncId
		});		
	}

	function changeState(state) {
		if (checkSelected() == false) return;
		
		if(state == "Delete" && confirm('确定要删除"' + selFuncName + '"吗？') == false) {
			return;
		}
		
		var url = "${ctx}/org/function/changeState.action?funcId=" + selFuncId + "&validState=" + state;
		$.get(url, function(data) {	    				    		
			if(data == "success") {
				refreshPage();
			} else {
				alert("错误: \n" + data);
			}
		})
		.error(function(xmlHttpRequest, textStatus, errorThrown) { 
			  alert("异常： " + xmlHttpRequest.status + ":" + errorThrown);
		});			
	}
	
	function selectApp() {
		$('#dialog_main').modal({
			keyboard: false,
			remote: '${ctx}/org/app/select.action'
		});
	}
	
	function onSelectAppEvent(appId) {
		$('#dialog_main').modal('hide');		
		loadPage(appId);	
	}		

	function checkSelected(cmd) {
		if ('${appId}' == '0') {
			alert("请选择应用");
			return false;			
		}
		
		if (cmd== 'add') {
			if (selFuncId == null) {
				alert("请选择上级节点");
				return false;
			}
		} else {
			if (selFuncId == null) {
				alert("请选择功能节点");
				return false;
			}
			if (selFuncId == 0) {
				alert("请选择非根节点");
				return false;
			}			
		}
		
		return true;
	}
	
	function dialogSubmitSuccess() {
		$('#dialog_main').modal('hide');
		refreshPage();
	}
	
    function refreshPage() {
		var url = "/org/function/manage.action?appId=${appId}";
		load_main_content(url);
    }
    
    function loadPage(appId) {
		var url = "/org/function/manage.action?appId=" + appId;
		load_main_content(url);
    }    
        
</script>

</head>
<body>

<div>
	<c:choose>
		<c:when test="${appId == 0}"><h3>应用管理</h3></c:when>
		<c:otherwise><h3>应用管理-${appName}</h3></c:otherwise>
	</c:choose>			
</div>

<nav class="navbar navbar-default" role="navigation">
  <ul class="nav navbar-nav navbar-left">
    <li>
      <a href="javascript:selectApp()">选择</a></li>  
    <li>
      <a href="javascript:addFunction()">新建</a></li>
    <li>
      <a href="javascript:editFunction()">编辑</a></li>
    <li>
      <a href="javascript:changeState('Delete')">删除</a></li>
  </ul>
</nav>

<div class="clearfix">
	<script>
		var function_org_setting = {
			check: {
				enable: false
			},
			data: {
				simpleData: {
					enable: true
				}
			},
	        callback: {
	            onClick: function_org_onTreeClick
	        }
		};
		
		var selFuncId = null;
		var selFuncName = null;
		
		function function_org_onTreeClick(event, treeId, treeNode, clickFlag) {
			selFuncId = treeNode.id;
			selFuncName = treeNode.name;
			
			//alert(selFuncId + ":" + selFuncName);
		}

		var function_org_Nodes = ${listFunction};
	
		$(document).ready(function(){
			$.fn.zTree.init($("#function_funcTreeView"), function_org_setting, function_org_Nodes);
		});
	</script>

	<ul id="function_funcTreeView" class="ztree"></ul>
</div>

<div class="clearfix">
	<div style="height:20px;background-color:white"></div>
</div>

</body>
</html>